package org.apache.ofbiz.services;

import com.ruoyi.doc.domain.ContentType;
import org.apache.ofbiz.base.util.Debug;
import org.apache.ofbiz.base.util.UtilGenerics;
import org.apache.ofbiz.base.util.UtilValidate;
import org.apache.ofbiz.entity.Delegator;
import org.apache.ofbiz.entity.GenericEntityException;
import org.apache.ofbiz.entity.GenericValue;
import org.apache.ofbiz.entity.condition.EntityCondition;
import org.apache.ofbiz.entity.condition.EntityOperator;
import org.apache.ofbiz.entity.util.EntityQuery;
import org.apache.ofbiz.service.DispatchContext;
import org.apache.ofbiz.service.GenericServiceException;
import org.apache.ofbiz.service.LocalDispatcher;
import org.apache.ofbiz.service.ServiceUtil;

import java.util.*;

/**
 * 北京XXX信息技术有限公司-版权所有
 *
 * @author yuanpingfang
 * @version 1.0
 * @Package org.apache.ofbiz.services
 * @ClassName ContentTypeServices
 * @Description ofbiz文档类型服务
 * @date 2021-4-17 10:11
 */
public class ContentTypeServices {

    public static final String module = ContentTypeServices.class.getName();

    /**
     * @param
     * @return java.util.Map<java.lang.String, java.lang.Object>
     * @Title selectContentTypeById
     * @Description 查询内容类型
     * @author yuanpingfang
     * @date 2021-4-17 10:14
     */
    public static Map<String, Object> selectContentTypeById(DispatchContext dctx, Map<String, Object> context) {
        Delegator delegator = dctx.getDelegator();
        //Locale locale = (Locale) context.get("locale");
        String contentTypeId = (String) context.get("contentTypeId");
        GenericValue contentTypeValue = null;
        try {
            contentTypeValue = EntityQuery.use(delegator).from("ContentType").where("contentTypeId", contentTypeId).queryFirst();
        } catch (GenericEntityException e) {
            Debug.logError(e, module);
            return ServiceUtil.returnError(e.getMessage());
        }
        Map<String, Object> returnMap = ServiceUtil.returnSuccess();
        returnMap.put("contentType", contentTypeValue);
        return returnMap;
    }

    /**
     * @param
     * @return java.util.Map<java.lang.String, java.lang.Object>
     * @Title selectContentTypeList
     * @Description 查询内容类型列表
     * @author yuanpingfang
     * @date 2021-4-17 10:13
     */
    public static Map<String, Object> selectContentTypeList(DispatchContext dctx, Map<String, Object> context) {
        LocalDispatcher dispatcher = dctx.getDispatcher();
        ContentType contentType = (ContentType) context.get("contentType");
        Integer pageNum = (Integer) context.get("pageNum");
        Integer pageSize = (Integer) context.get("pageSize");
        String orderBy = (String) context.get("orderBy");

        if(UtilValidate.isEmpty(orderBy)){
            orderBy = "-lastUpdatedStamp";
        }
        List<EntityCondition> conditions = new ArrayList<>();
        if(UtilValidate.isNotEmpty(contentType.getContentTypeId())){
            conditions.add(EntityCondition.makeCondition("contentTypeId", EntityOperator.EQUALS, contentType.getContentTypeId()));
        }
        if(UtilValidate.isNotEmpty(contentType.getDescription())){
            conditions.add(EntityCondition.makeCondition("description", EntityOperator.LIKE, "%" + contentType.getDescription() + "%"));
        }
        Map<String, Object> queryMap = new HashMap<>();
        queryMap.put("entityName","ContentType");
        queryMap.put("pageNum",pageNum);
        queryMap.put("pageSize",pageSize);
        queryMap.put("queryCondition",EntityCondition.makeCondition(conditions));
        queryMap.put("orderBy",orderBy);// 多个排序字段 "|" 符连接
        queryMap.put("fields","");// 多个查询字段 "|" 符连接
        //queryMap.put("distinct","Y");
        Map<String, Object> queryPageMap = null;
        try {
            queryPageMap = dispatcher.runSync("queryPageList", queryMap);
        } catch (GenericServiceException e) {
            Debug.logError(e, module);
            return ServiceUtil.returnError(e.getMessage());
        }
        return queryPageMap;
    }

    /**
     * @Title deleteContentTypeByIds
     * @Description 批量删除内容类型服务
     * @param 
     * @return java.util.Map<java.lang.String,java.lang.Object>
     * @author yuanpingfang
     * @date 2021-4-17 13:08
     */
    public static Map<String, Object> deleteContentTypeByIds(DispatchContext dctx, Map<String, Object> context) {
        Delegator delegator = dctx.getDelegator();
        List contentTypeIds = UtilGenerics.checkList(context.get("contentTypeIds"));
        try {
            delegator.removeByCondition("ContentType",EntityCondition.makeCondition("contentTypeId", EntityOperator.IN,contentTypeIds));
        } catch (GenericEntityException e) {
            Debug.logError(e, module);
            return ServiceUtil.returnError(e.getMessage());
        }
        return ServiceUtil.returnSuccess();
    }





}
